package com.xmall.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmall.product.define.dto.group.GroupDto;
import com.xmall.product.define.dto.spu.SpuBaseAttrDto;
import com.xmall.product.define.request.spu.SpuBaseAttrRequest;
import com.xmall.product.entity.SpuBaseAttr;
import com.xmall.product.mapper.SpuBaseAttrMapper;
import com.xmall.product.service.ISpuBaseAttrService;
import com.xmall.product.utils.ConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 商品spu属性 服务实现类
 */
@Service
public class SpuBaseAttrServiceImpl extends ServiceImpl<SpuBaseAttrMapper, SpuBaseAttr> implements ISpuBaseAttrService {

    /**
     * 保存
     */
    @Override
    public void save(Long spuId, List<SpuBaseAttrRequest> baseAttrs) {
        baseAttrs.forEach(baseAttr -> baseAttr.setSpuId(spuId));
        if(CollectionUtil.isNotEmpty(baseAttrs)){
            this.saveBatch(ConvertUtils.SpuBaseAttrModule.INSTANCE.toEntityList(baseAttrs));
        }
    }

    /**
     * 更新
     */
    @Transactional
    @Override
    public void update(Long spuId, List<SpuBaseAttrRequest> baseAttrs) {
        this.remove(new QueryWrapper<SpuBaseAttr>().eq("spu_id", spuId));
        this.save(spuId, baseAttrs);
    }

    /**
     * 删除
     */
    @Override
    public void delete(Long spuId) {
        this.remove(new QueryWrapper<SpuBaseAttr>().eq("spu_id", spuId));
    }

    /**
     * 可检索列表
     */
    @Override
    public List<SpuBaseAttrDto> searchableList(Long spuId) {
        return ConvertUtils.SpuBaseAttrModule.INSTANCE.toDtoList(this.baseMapper.searchableList(spuId));
    }

    /**
     * 填充属性值
     */
    @Override
    public void fillValue(Long spuId, List<GroupDto> groupAttrs) {
        this.setAttrValue(spuId, groupAttrs);
    }

    /**
     * 填充属性值（去掉没有值的项）
     */
    @Override
    public List<GroupDto> fillValueAndRemoveEmpty(Long spuId, List<GroupDto> groupList) {
        this.setAttrValue(spuId, groupList);

        return groupList.stream().map(group -> {
            group.setAttrs(
                group.getAttrs().stream()
                    .filter(attr -> StrUtil.isNotBlank(attr.getValue()))
                    .collect(Collectors.toList())
            );
            return group;
        }).filter(groupAttrDto -> groupAttrDto.getAttrs().size() > 0)
        .collect(Collectors.toList());
    }

    private void setAttrValue(Long spuId, List<GroupDto> groupList) {
        Map<Long, SpuBaseAttr> spuBaseAttrMap = this.list(new QueryWrapper<SpuBaseAttr>().eq("spu_id", spuId)).stream().collect(Collectors.toMap(SpuBaseAttr::getAttrId, Function.identity(), (key1, key2) -> key2));
        groupList.stream().forEach(group ->
            group.getAttrs().stream().forEach(attr -> {
                SpuBaseAttr spuBaseAttr = spuBaseAttrMap.get(attr.getId());
                if(null != spuBaseAttr && StrUtil.isNotBlank(spuBaseAttr.getAttrValue())) {
                    attr.setValue(spuBaseAttr.getAttrValue());
                    attr.setValues(Arrays.asList(spuBaseAttr.getAttrValue().split(",")));
                }
            })
        );
    }
}
